﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.DataModel;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using Tracy.Messages;
using System.Collections.Specialized;

namespace Tracy.Plugins.DownloadAdapters
{
    public class ThunderAdapter : IDownloadAdapter
    {
        private Type _thunderAgentType = Type.GetTypeFromProgID("ThunderAgent.Agent");
        private string _applicationFullPath;
        private object _lockHelper = new object();
        private void CheckRegistry()
        {
            RegistryKey regdir = Registry.CurrentUser.OpenSubKey("SOFTWARE", false);
            regdir = regdir.OpenSubKey("Thunder Network", false);
            if (regdir != null)
            {
                regdir = regdir.OpenSubKey("Thunder", false);
                if (regdir != null)
                {
                    object path = regdir.GetValue("Path");
                    if (path != null)
                    {
                        _applicationFullPath = path.ToString();
                        if (!File.Exists(_applicationFullPath)) _applicationFullPath = null;
                    }
                }
            }
        }

        public ThunderAdapter()
        {
            CheckRegistry();
        }
        
        public void Init(StringDictionary parameters) { }

        public void Download(Resource resource, string defaultPath)
        {
            if (resource == null) return;
            if (IsResourceTypeSupported(resource.GetResourceType()))
            {
                if (Process.GetProcessesByName("Thunder").Length == 0)
                {
                    lock (_lockHelper)
                    {
                        //Start thunder if not running
                        if (Process.GetProcessesByName("Thunder").Length == 0)
                        {
                            Process.Start(_applicationFullPath);
                            System.Threading.Thread.Sleep(3000);
                        }
                    }
                }
                dynamic agent = System.Activator.CreateInstance(_thunderAgentType);
                string description = "Sent from Tracy\n" + resource.Title;
                agent.AddTask(resource.Url, null, defaultPath, description, "", -1, 0, -1);

                //Will not simulate UI interaction
                //if (autoMode)
                //{
                //    TaskMessage taskMsg = new TaskMessage(this, typeof(ThunderNewTaskDialogHandler), resource, null);
                //    TracyBus.Instance.MessageDispatcher.EnqueueMessage(taskMsg);
                //}

                agent.CommitTasks();
            }
        }


        public bool IsAvailable()
        {
            return (_thunderAgentType != null && _applicationFullPath != null);
        }

        public bool IsResourceTypeSupported(string type)
        {
            if (IsAvailable())
            {
                if (type == "BT") return true;
                if (type == "Emule") return true;
            }
            return false;
        }

        public string[] GetTempFileExts(string type)
        {
            if (type == "BT") return new string[]{".bt.td"};
            if (type == "Emule") return new string[] { ".emule.td", ".td" }; ;
            return null;
        }

        public string GetValidFileName(string fileName)
        {
            if (fileName == null) return null;
            return fileName.Replace("?", "_").Replace("\\", "_").Replace("/", "_").Replace("*", "_").Replace("\"", "_").Replace(":", "_").Replace("<", "_").Replace(">", "_");
        }


        public string GetTempDownloadFolder(string preferredFolder)
        {
            return preferredFolder;
        }
    }
}
            
